package com.example.tencentimdemo

import android.app.Application
import com.tencent.imsdk.v2.*

class App : Application() {

    private val sdkListener by lazy {  MyV2TIMSdkListener() }

    override fun onCreate() {
        super.onCreate()

        initTencentIM()
    }


    private fun initTencentIM() {

        //初始化Config对象
        val config = V2TIMSDKConfig()
        //指定log输出级别
        config.logLevel = V2TIMSDKConfig.V2TIM_LOG_INFO
        //指定log监听器
        config.logListener = MyV2TIMLogListener()

        //连接监听器
        V2TIMManager.getInstance().addIMSDKListener(sdkListener)

        //初始化IM SDK
        V2TIMManager.getInstance().initSDK(this,1400634502,config)

    }





    /** 反初始化 */
    private fun unInitTencentIM(){
        V2TIMManager.getInstance().removeIMSDKListener(sdkListener)
        V2TIMManager.getInstance().unInitSDK()
    }



    /**
     * 日志监听器
     */
    private class MyV2TIMLogListener : V2TIMLogListener() {
        override fun onLog(logLevel: Int, logContent: String?) {
            println("V2TIM Log \n:  $logContent")
        }
    }
    /** Sdk 链接监听器 */
    private class  MyV2TIMSdkListener : V2TIMSDKListener(){
        //正在连接到腾讯云服务器
        override fun onConnecting() {
            super.onConnecting()
        }
        //已经成功连接到腾讯云服务器
        override fun onConnectSuccess() {
            super.onConnectSuccess()
        }
        //连接腾讯云服务器失败
        override fun onConnectFailed(code: Int, error: String?) {
            super.onConnectFailed(code, error)
        }
        //当前用户被踢下线
        override fun onKickedOffline() {
            super.onKickedOffline()
        }
        //登录票据已经过期
        override fun onUserSigExpired() {
            super.onUserSigExpired()
        }
        //当前用户的资料发生了更新
        override fun onSelfInfoUpdated(info: V2TIMUserFullInfo?) {
            super.onSelfInfoUpdated(info)
        }
    }
}